package com.beiding.test.pg;

import com.beiding.sqlbuilder.CompareUnit;
import com.beiding.sqlbuilder.ConditionUnit;
import com.beiding.sqlbuilder.ConditionUnitHandler;
import com.beiding.sqlbuilder.ParameterHolder;

import java.util.List;

public class CompareUnitHandler implements ConditionUnitHandler {

    @Override
    public String handle(ConditionUnit conditionUnit) {
        CompareUnit compareUnit = (CompareUnit) conditionUnit;
        List<ParameterHolder> parameters = compareUnit.getParameters();
        switch (compareUnit.getType()) {
            case Eq:
                return twoParam(parameters, "=");
            case In:
                return twoParam(parameters, "in");
        }

        throw new RuntimeException("暂不支持比较类型:" + compareUnit.getType());
    }

    private String twoParam(List<ParameterHolder> parameters, String type) {
        return parameters.get(0).execute() + " " + type + " " + parameters.get(1).execute();
    }

    @Override
    public boolean canHandle(ConditionUnit conditionUnit) {
        return conditionUnit instanceof CompareUnit;
    }
}
